//
//  PopoverPickerviewController.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-15.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "PopoverPickerviewController.h"
#import "NSString+Additions.h"

@implementation PopoverPickerviewController

- (id)initWithNibName:(NSString *)nibNameOrNil number:(NSString *)numberString{
    self = [super initWithNibName:nibNameOrNil bundle:nil];
    if (self) {
        numberArray = [NSMutableArray new];
        [numberArray addObject:[NSNumber numberWithInt:50]];
        [numberArray addObject:[NSNumber numberWithInt:10]];
        [numberArray addObject:[NSNumber numberWithInt:10]];
        
        UIPickerView *picker = (UIPickerView *)[self.view viewWithTag:TagOfUIPickerView];
        float number = [numberString floatValue];
        NSInteger row1 = (int)floorf(number);
        numberString = [numberString replaceFrom:[NSString stringWithFormat:@"%d.",row1] To:@""];
        int row23 = [numberString intValue];        
        NSInteger row2 = row23/10;
        NSInteger row3 = row23%10;
        MyNSLog(@"[%d,%d,%d]", row1,row2,row3);
        [picker selectRow:row1 inComponent:0 animated:NO];
        [picker selectRow:row2 inComponent:1 animated:NO];
        [picker selectRow:row3 inComponent:2 animated:NO];
    }
    return self;
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

#pragma mark - Picker dataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return numberArray.count;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [(NSNumber *)[numberArray objectAtIndex:component] intValue];
}

#pragma mark - Picker delegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) { 
        if (row<10) {
            return [NSString stringWithFormat:@"  %d",row];
        }else{
            return [NSString stringWithFormat:@"%d",row];
        }
    }
    return [NSString stringWithFormat:@"%d",row];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (component == 0) {
        return 60;
    }
    return 45;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSInteger row1 = [pickerView selectedRowInComponent:0];
    NSInteger row2 = [pickerView selectedRowInComponent:1];
    NSInteger row3 = [pickerView selectedRowInComponent:2];
    NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d.%d%d",row1,row2,row3], @"number", nil];
    postNWithInfos(@"SysMsg_FullScreenMode_changeBtnNum", self, infos);
}

#pragma mark - View lifecycle
-(void)dealloc{
    MyNSLog(@"PopoverPickerviewController is deallocing...");
    [numberArray release];
    numberArray = nil;
    [super dealloc];
}
@end
